package Both;

import Both.TypeTransport;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import Serveur.Serialization;

/**
 * Cette classe permet de gerer les moyens de transport
 * 
 * 
 */
public class Transport implements Serializable {

    // Type du transport
    private TypeTransport type;

    // Numero du transport
    private int numTrans;

    // Capacite du moyen de transport
    private int capacite;

    /**
     * Constructeur par defaut (pour le XML)
     */
    public Transport() {
    }

    /**
     * Constructeur normal
     * 
     * @param nom
     * @param description
     * @param capacite
     * 
     */
    public Transport(TypeTransport type, int num, int capacite) {
        // remplacer les / present dans le code
        type.toString().replace(Serialization.CAR_SPACE,
                Serialization.CAR_REPLACE);

        this.capacite = capacite;
        numTrans = num;

        this.type = type;

    }

    @Override
    public String toString() {
        String retour;
        retour = "Type Transport : " + type.getTransportString();
        retour += "\tNumero : " + numTrans;
        retour += "\tCapacite : " + capacite;
        return retour;
    }

    /**
     * affiche le données on utilisant la methode toString
     * 
     */

    public void print() {
        System.out.println(toString());
    }

    @Override
    public boolean equals(Object arg0) {
        // TODO Auto-generated method stub
        Transport trans = (Transport) arg0;
        if (!(trans.getCapacite() == capacite)) {
            return false;
        }
        if (!trans.getType().equals(type)) {
            return false;
        }

        return true;

    }

    /**
     * retourne le Type du transport
     * 
     * @return TypeTransport
     */

    public TypeTransport getType() {
        return type;
    }

    /**
     * Change le type du transport
     * 
     * @param type
     */

    public void setType(TypeTransport type) {
        this.type = type;
    }

    /**
     * retourne la capacité
     * 
     * @return capacite
     */

    public int getCapacite() {
        return capacite;
    }

    /**
     * change la capacité
     * 
     * @param cap
     */
    public void setCapacite(int cap) {
        this.capacite = cap;
    }

    /**
     * Rechercher un Transport dans le serveur
     * 
     * @param ListeTransport
     * @param nom
     * @return
     */
    public static ArrayList<Transport> searchTransport(
            ArrayList<Transport> ListeTransport, TypeTransport type) {
        ArrayList<Transport> ListeRetour = new ArrayList<Transport>();

        Iterator<Transport> it = ListeTransport.iterator();
        while (it.hasNext()) {
            Transport current = it.next();
            if (current.getType().equals(type)) {
                ListeRetour.add(current);
            }
        }
        return ListeRetour;
    }

    /**
     * retourne le numero de transport
     * 
     * @return numTrans
     */
    public int getNumTrans() {
        return numTrans;
    }

    /**
     * change le numero de Transport
     * 
     * @param numTrans
     */
    public void setNumTrans(int numTrans) {
        this.numTrans = numTrans;
    }

    /**
     * Rechercher un transport dans le serveur
     * 
     * @param ListeTransport
     * @param T
     * @return
     */
    public static ArrayList<Transport> searchTransport(
            ArrayList<Transport> ListeTransport, Transport T) {
        ArrayList<Transport> ListeRetour = new ArrayList<Transport>();

        Iterator<Transport> it = ListeTransport.iterator();
        while (it.hasNext()) {
            Transport current = it.next();
            if (current.equals(T)) {
                ListeRetour.add(current);
            }
        }

        return ListeRetour;
    }

    /**
     * Recherche un transport par son numero
     * 
     */
    public static Transport getTransport(ArrayList<Transport> ListeTranport,
            int num) {
        Iterator<Transport> it = ListeTranport.iterator();
        while (it.hasNext()) {
            Transport t = it.next();
            if (t.getNumTrans() == num) {
                return t;
            }
        }
        return null;
    }

    /**
     * @return the typeString
     */
    /*
     * public String getTypeString() { return typeString; }
     */

    /**
     * @param typeString
     *            the typeString to set
     */
    /*
     * public void setTypeString(String typeString) { this.typeString =
     * typeString; this.type = TypeTransport.valueOf(typeString); }
     */
}
